<script setup>
import * as topicApi from "@/api/topicApi.js";
import { ElMessage } from "element-plus";
import { ref } from "vue";

const topicListRef = ref([]);
const totalRef = ref(0);

fetchTopicList()

function fetchTopicList() {
  topicApi.findByPage().then(({ code, data, msg }) => {
    topicListRef.value = data.data;
    totalRef.value = data.total;
  });
}

async function handleDel({ id }) {
  const { code, msg } = await topicApi.delTopic(id)
  if (code === 2000) {
    ElMessage.success(msg)
    fetchTopicList()
  }
}

</script>

<template>
  <div class="topic-list-container">
    <el-table :data="topicListRef" style="width: 100%">
      <el-table-column prop="title" label="题干" width="180" />
      <el-table-column prop="analyze" label="解析" width="180" />
      <el-table-column prop="score" label="分数" />
      <el-table-column prop="score" label="分数">
        <template #default="scope">
          <el-button link type="danger" size="small" @click="handleDel(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<style scoped></style>